/*
 *  Getris -- A Tetris game written in Groovy.
 *  Copyright (C) 2008 Johnny Jian
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package cn.johnnyjian.getris.panel

import java.awt.Color
import java.awt.Graphics

class SquarePanel extends FramePanel {

	def containerWidth
	def containerHeight
	def squareSize
	def colors

	SquarePanel(width, height, size) {
		containerWidth = width
		containerHeight = height
		squareSize = size
		colors = new Color[height][width]

		setSize(width * size + 2, height * size + 2)
	}

	void paint(Graphics g) {
		super.paint(g)
		drawAllSquares(g)
	}

	protected drawAllSquares(Graphics g) {
		colors?.eachWithIndex { row, y ->
			row.eachWithIndex { c, x ->
				if (c) {
					drawSquareAt x, y, g
				}
			}
		}
	}

	protected drawSquareAt(x, y, g = graphics) {
		if (colors[y][x] == null) {
			g.color = BACKGROUND_COLOR.darker()
			g.fillRect x * squareSize + 1, y * squareSize + 1, squareSize, squareSize
		} else {
			g.color = colors[y][x]
			g.fill3DRect x * squareSize + 1, y * squareSize + 1, squareSize, squareSize, true
		}
	}

}
